﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BAL.EstRec.FDVGrid;

namespace BAL.EstRec.FDVTotalRecurso
{
    public class ModelCapFdvTotalRecurso
    {
        public CapFDVTotalRecursoCollection entry;

        public ModelCapFdvTotalRecurso(CapturasPFDV cpfdv)
        {
            this.entry = new CapFDVTotalRecursoCollection();

            if (cpfdv != null)
            {
                var res2 = (from o in cpfdv.ObCollPFDV
                            orderby o.recurso.idRecurso
                            select
                             o.recurso.idRecurso).Distinct().ToList();
                foreach (string r in res2)
                {
                    CapFDVTotalRecurso cfTotalRecurso = this.calculate(r, cpfdv);
                    this.entry.Add(cfTotalRecurso);
                }
            }

        }

        //20121206
        public ModelCapFdvTotalRecurso(CapturasPFDV cpfdv, BAL.EstRec.FDVIndicador.ModelCapFdvIndicador mi)
        {
            this.entry = new CapFDVTotalRecursoCollection();

            if (cpfdv != null)
            {
                var res2 = (from o in cpfdv.ObCollPFDV
                            orderby o.recurso.idRecurso
                            select
                             o.recurso.idRecurso).Distinct().ToList();
                foreach (string r in res2)
                {
                    //var resInd = (from o in mi.entry
                    //              where o.Iniciativas. == r
                    //              select o).First();

                    CapFDVTotalRecurso cfTotalRecurso = this.calculate(r, cpfdv);
                    this.entry.Add(cfTotalRecurso);
                }
            }

        }

        public CapFDVTotalRecurso calculate(string idRecurso, CapturasPFDV cpfdv)
        {
            CapFDVTotalRecurso cfTotalRecurso;

            //Calculo de visitas sugeridas
            var visitasSugeridas = (from o in cpfdv.ObCollPFDV
                                   where o.recurso.idRecurso == idRecurso && o.cantidad > 0
                                   select o.recurso.visitasSugeridas).Sum();
            //Tiempo implementacion
            var tiempoImplementacion = (from o in cpfdv.ObCollPFDV
                                        where o.recurso.idRecurso == idRecurso
                                        select o.recurso.tiempoImplementacion * o.cantidad).Sum();
            //Suma recursos
            var recursos = (from o in cpfdv.ObCollPFDV
                                        where o.recurso.idRecurso == idRecurso
                                        select o.cantidad).Sum();

            
            cfTotalRecurso = new CapFDVTotalRecurso(new Recurso(idRecurso, idRecurso), tiempoImplementacion, visitasSugeridas,0,recursos);
            return cfTotalRecurso;
        }
    }
}
